<!--
 * @Description: 菜单管理-编辑菜单
 * @Author: xinqiao lxq1990.0716@qq.com
 * @Date: 2024-03-05
 * @LastEditors: xinqiao lxq1990.0716@qq.com
 * @LastEditTime: 2024-09-25
-->
<script lang="ts" setup>
  import { Form, message } from 'ant-design-vue'
  import { computed, reactive, watch } from 'vue'

  import { requestGetPermissionById, requestUpdatePermission } from '/@/api/system/ysys-user/permission'
  import { HttpCodeEnum } from '/@/enums/httpEnum'
  import { getModuleList } from '/@/storage'

  const props = defineProps({
    visible: { type: Boolean, default: false },
    permissionId: { type: Number, default: NaN },
  })

  const emits = defineEmits(['update:visible', 'success'])

  const visible = computed({
    get: () => props.visible,
    set: (val) => emits('update:visible', val),
  })

  watch(
    () => visible.value,
    (newVal) => {
      if (newVal) onPageshow()
      else onPageHide()
    },
  )

  /**
   * 页面显示
   */
  const onPageshow = () => initData()

  /**
   * 页面关闭
   */
  const onPageHide = () => resetFields()

  /** 模块id */
  const moduleId = getModuleList()[0].moduleId

  /**
   * 初始化数据
   */
  const initData = async () => {
    const { responseData } = await requestGetPermissionById({ permissionId: props.permissionId })
    const { permissionName, permissionWeight, permissionUrl, permissionDesc, permissionPId, permissionId, permissionType } = responseData
    form.state.permissionName = permissionName
    form.state.permissionWeight = permissionWeight
    form.state.permissionUrl = permissionUrl
    form.state.permissionDesc = permissionDesc
    form.state.permissionPId = permissionPId
    form.state.permissionId = permissionId
    form.state.permissionType = permissionType
  }

  /** 表单配置 */
  const form = reactive({
    state: {
      /** 名称 */
      permissionName: '',
      /** 权重 */
      permissionWeight: NaN,
      /** 路由 */
      permissionUrl: '',
      /** 描述 */
      permissionDesc: '',
      /** 父级菜单id */
      permissionPId: null,
      /** 菜单id */
      permissionId: null,
      /** 菜单类型 */
      permissionType: null,
    },
    rule: {
      permissionName: [{ required: true, message: '请输入名称' }],
      permissionWeight: [{ required: true, message: '请输入权重' }],
    },
    onOk: () => {
      validate()
        .then(async () => {
          const { permissionName, permissionWeight, permissionUrl, permissionDesc, permissionPId, permissionId, permissionType } = form.state
          const { responseCode } = await requestUpdatePermission({ moduleId, permissionPId, permissionId, permissionType, permissionName, permissionWeight, permissionUrl, permissionDesc })
          if (responseCode === HttpCodeEnum.SUCCESS) {
            message.success('修改成功')
            visible.value = false
            emits('success')
          }
        })
        .catch(() => {})
    },
  })
  /** 表单hook */
  const { validate, validateInfos, resetFields } = Form.useForm(form.state, form.rule)
</script>

<template>
  <div class="edit">
    <a-modal v-model:visible="visible" title="编辑菜单" @ok="form.onOk">
      <a-form :label-col="{ span: 6 }" :wrapper-col="{ span: 16 }">
        <a-form-item label="名称" v-bind="validateInfos.permissionName">
          <a-input v-model:value="form.state.permissionName" placeholder="请输入" />
        </a-form-item>
        <a-form-item label="权重" v-bind="validateInfos.permissionWeight">
          <a-input type="number" v-model:value="form.state.permissionWeight" placeholder="请输入" />
        </a-form-item>
        <a-form-item label="路由" v-bind="validateInfos.permissionUrl">
          <a-input v-model:value="form.state.permissionUrl" placeholder="请输入" />
        </a-form-item>
        <a-form-item label="备注" v-bind="validateInfos.permissionDesc">
          <a-input v-model:value="form.state.permissionDesc" placeholder="请输入" />
        </a-form-item>
      </a-form>
    </a-modal>
  </div>
</template>

<style lang="less" scoped></style>
